package com.example.xcsubsystem.service;

import com.example.xcsubsystem.entity.ResourceEntity;
import com.example.xcsubsystem.entity.User;
import com.example.xcsubsystem.entity.UserResourceEntity;
import com.example.xcsubsystem.repository.ResourceRepository;
import com.example.xcsubsystem.repository.UserRepository;
import com.example.xcsubsystem.repository.UserResourceRepository;
import com.example.xcsubsystem.vo.ServiceStatusVO;
import com.example.xcsubsystem.vo.UserServiceVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 2024/9/28
 * 本系统内部Service层
 * @author fengyu
 */
@Service
public class LocalService {
    private static final Logger log = LoggerFactory.getLogger(LocalService.class);

    @Resource
    private UserRepository userRepository;

    @Resource
    private ResourceRepository restServiceRepository;

    @Resource
    private UserResourceRepository userServiceRepository;

    /**
     * 更新用户访问系统的权限
     * @param zzdID 用户浙政钉ID
     * @param stauts 是否允许访问系统
     * @return 成功返回true
     */
    public boolean empower(@Nonnull final String zzdID, final boolean stauts){
        // 首先判断用户是否存在，不存在就新建用户
        if(!userRepository.existsUserByAccountId(zzdID)) {
            log.info("浙政钉用户ID{}入库", zzdID);
            User user = new User();
            user.setAccountId(zzdID);
            user.setDept("某组织");
            user.setName("浙政钉用户XXX");
            user.setStatus(stauts ? 1 : 0);
            userRepository.saveAndFlush(user);
            return user.getId() > 0;
        } else {
            int r = userRepository.updateUserStatusByAccountId(zzdID, stauts ? 1 : 0);
            return 0 == r;
        }
    }

    /**
     * 关联统一平台的服务id
     * @param id 统一平台生成的ID
     * @param oid 服务的原id
     * @return 返回false时表示服务不存在
     */
    public boolean registService(String id, long oid) throws Exception{
        boolean exist = restServiceRepository.existsById(oid);
        if (!exist) {
            log.info("服务不存在：{}", oid);
            throw new Exception("服务不存在");
        }
        int r = restServiceRepository.updateRemoteId(id, oid);

        return r > 0;
    }

    /**
     * 获取所有在同一平台上注册过的服务在线状态
     * @return
     */
    public List<ServiceStatusVO> getServiceStatus(){
        List<ServiceStatusVO> list = restServiceRepository.findAllStatus();
        return list;
    }

    /**
     * 用户申请服务
     * @param accountId 浙政钉id
     * @param id 服务的id
     * @return
     */
    public boolean applyService(String accountId, long id){
        // 先判断服务是否存在
        boolean exist = restServiceRepository.existsById(id);
        if (!exist) return false;
        ResourceEntity rs = restServiceRepository.findById(id).get();
        UserResourceEntity us = new UserResourceEntity();
        log.info("dddddddddddd ",rs.getRemoteId());
        us.setRemoteId(rs.getRemoteId());
        us.setAccountId(accountId);

        us.setStatus(1);
        userServiceRepository.save(us);
        return true;
    }

    /**
     * *
     * @param accountId
     * @return
     */
    public List<UserServiceVO> getUserServiceStatus(String accountId){
        //
        List<UserServiceVO> list = userServiceRepository.findAllByAccountIdMatches(accountId);
        return list;
    }
}
